package com.baizhi.service.impl;

import com.baizhi.dao.CmfzArticleDao;
import com.baizhi.entity.CmfzArticle;
import com.baizhi.service.CmfzArticleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Slf4j
@Service
@Transactional(readOnly = true)
public class CmfzArticleServiceImpl implements CmfzArticleService {
    @Autowired
    private CmfzArticleDao cmfzArticleDao;
    @Override
    public List<CmfzArticle> selectArticle(int size) {
        //查询最新的n篇文章  这里用条件加分页查询
        Page<CmfzArticle> cmfzArticlePage = new Page<>(1, size);
        QueryWrapper<CmfzArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("article_date");
        Page<CmfzArticle> selectPage = cmfzArticleDao.selectPage(cmfzArticlePage, queryWrapper);
        List<CmfzArticle> cmfzArticles = selectPage.getRecords();
        log.debug("查询最新文章结果："+cmfzArticles);
        return cmfzArticles;
    }

    @Override
    //type=0   查询当前用户关注的上师的文章
    //type=1   查询非当前用户关注的上师的文章
    public List<CmfzArticle> selectArticleByGuruId(String type, int guruId) {
        QueryWrapper<CmfzArticle> queryWrapper = new QueryWrapper<>();
        if(type.equals("yes")){
            //此时查询条件 等于一个上师ID
            queryWrapper.eq("guru_id",guruId);
        }else if(type.equals("no")){
            //此时查询条件 不等于一个上师ID
            queryWrapper.ne("guru_id",guruId);
        }
        List<CmfzArticle> cmfzArticles = cmfzArticleDao.selectList(queryWrapper);
        log.debug("查询上师文章结果："+cmfzArticles);
        return cmfzArticles;
    }
}
